{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ec9oXJywSN6t"
   },
   "source": [
    "# Get data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "executionInfo": {
     "elapsed": 357,
     "status": "ok",
     "timestamp": 1623207955855,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "zm4hvBwauWXI",
    "outputId": "f53ba8a3-9fec-4c76-995a-a4e18cf192c5"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>image_name</th>\n",
       "      <th>BoxesString</th>\n",
       "      <th>domain</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7b73239dfd89b06c03e1be81cc5074ec47ae048305ec63...</td>\n",
       "      <td>949 967 999 994;368 649 443 685;118 280 185 33...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0e37ccf64ccbd456f07dcfe110133018f324e5bdf63b08...</td>\n",
       "      <td>691 60 730 119;838 901 897 969;477 444 526 489...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>89e0aa4148f0a9ff01e9e55c5a2bcbe294150444dbe43c...</td>\n",
       "      <td>953 178 1023 244;265 164 326 228;657 790 689 8...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>fed8dde94c7a51de782b71f45c9115cdf7208cf7f33eee...</td>\n",
       "      <td>801 402 882 459;214 924 271 1023;299 726 360 7...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>8a593fc7894c37145d302bce36822bf21725088cf17b23...</td>\n",
       "      <td>187 887 294 1003;631 343 679 386;337 719 402 7...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          image_name  ... domain\n",
       "0  7b73239dfd89b06c03e1be81cc5074ec47ae048305ec63...  ...      0\n",
       "1  0e37ccf64ccbd456f07dcfe110133018f324e5bdf63b08...  ...      0\n",
       "2  89e0aa4148f0a9ff01e9e55c5a2bcbe294150444dbe43c...  ...      0\n",
       "3  fed8dde94c7a51de782b71f45c9115cdf7208cf7f33eee...  ...      0\n",
       "4  8a593fc7894c37145d302bce36822bf21725088cf17b23...  ...      0\n",
       "\n",
       "[5 rows x 3 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "train_df = pd.read_csv(\"clean_train.csv\", index_col=0)\n",
    "train_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 303,
     "status": "ok",
     "timestamp": 1623208076284,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "K7q-K-iju8aU",
    "outputId": "28b3870b-0027-430e-8b71-1f7d4bc843b1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3655"
      ]
     },
     "execution_count": 6,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(train_df[\"domain\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 305,
     "status": "ok",
     "timestamp": 1623208090563,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "lr7fFsoQva42",
    "outputId": "b2ae6d28-d595-48a9-fa8b-192b988b25b1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12    747\n",
       "15    588\n",
       "5     448\n",
       "17    432\n",
       "14    401\n",
       "10    204\n",
       "2     176\n",
       "6     160\n",
       "16     98\n",
       "9      82\n",
       "13     66\n",
       "1      60\n",
       "7      60\n",
       "8      32\n",
       "11     30\n",
       "0      27\n",
       "4      24\n",
       "3      20\n",
       "Name: domain, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df[\"domain\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "YU1d9a7iwMKG"
   },
   "outputs": [],
   "source": [
    "count_dict = dict(train_df[\"domain\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 309,
     "status": "ok",
     "timestamp": 1623208859977,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "Wctxs5w4yYOa",
    "outputId": "59bd0d30-d6d2-456f-fd9c-3497842b9d6d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 24,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(count_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "GxKrWZ_wvGpR"
   },
   "source": [
    "# Find a split strategy\n",
    "A strategy that balances the 4 folds using random search."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "0bYV38HwwdCi"
   },
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "\n",
    "def get_split(bound=10):\n",
    "    target = 3655 / 4\n",
    "\n",
    "    while True:\n",
    "        domain_list = [i for i in range(18)]\n",
    "        domains_0 = []\n",
    "        domains_1 = []\n",
    "        domains_2 = []\n",
    "        domains_3 = []\n",
    "        for _ in range(4):\n",
    "            domain = random.choice(domain_list)\n",
    "            domain_list.remove(domain)\n",
    "            domains_0.append(domain)\n",
    "        for _ in range(4):\n",
    "            domain = random.choice(domain_list)\n",
    "            domain_list.remove(domain)\n",
    "            domains_1.append(domain)\n",
    "        for _ in range(5):\n",
    "            domain = random.choice(domain_list)\n",
    "            domain_list.remove(domain)\n",
    "            domains_2.append(domain)\n",
    "        for _ in range(5):\n",
    "            domain = random.choice(domain_list)\n",
    "            domain_list.remove(domain)\n",
    "            domains_3.append(domain)\n",
    "\n",
    "    def get_count(l):\n",
    "        result = 0\n",
    "        for element in l:\n",
    "            result += count_dict[element]\n",
    "        return target - result\n",
    "\n",
    "    if (\n",
    "        -bound < get_count(domains_0) < bound\n",
    "        and -bound < get_count(domains_1) < bound\n",
    "        and -bound < get_count(domains_2) < bound\n",
    "        and -bound < get_count(domains_3) < bound\n",
    "    ):\n",
    "        print(domains_0, domains_1, domains_2, domains_3)\n",
    "        print(\n",
    "            get_count(domains_0),\n",
    "            get_count(domains_1),\n",
    "            get_count(domains_2),\n",
    "            get_count(domains_3),\n",
    "        )\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 77477,
     "status": "ok",
     "timestamp": 1623210314850,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "O9tv93QD3n2h",
    "outputId": "702666fa-5dd7-4696-f2cb-4be138ebc48d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[15, 13, 6, 16] [12, 9, 1, 0] [3, 17, 11, 14, 8] [2, 7, 5, 4, 10]\n",
      "1.75 -2.25 -1.25 1.75\n"
     ]
    }
   ],
   "source": [
    "get_split(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "z-6EAP0x4WwK"
   },
   "outputs": [],
   "source": [
    "domains_0, domains_1, domains_2, domains_3 = (\n",
    "    [15, 13, 6, 16],\n",
    "    [12, 9, 1, 0],\n",
    "    [3, 17, 11, 14, 8],\n",
    "    [2, 7, 5, 4, 10],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 370,
     "status": "ok",
     "timestamp": 1623212183010,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "VSwOBIQh-h6l",
    "outputId": "9034c024-1a4a-49ad-e8b3-01da0fd6b62c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "912"
      ]
     },
     "execution_count": 51,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "indexes_0 = []\n",
    "for domain in domains_0:\n",
    "    indexes_0 += list(train_df.index[train_df[\"domain\"] == domain])\n",
    "len(indexes_0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "executionInfo": {
     "elapsed": 379,
     "status": "ok",
     "timestamp": 1623212237252,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "jLIOraGg_L7M"
   },
   "outputs": [],
   "source": [
    "indexes_1 = []\n",
    "for domain in domains_1:\n",
    "    indexes_1 += list(train_df.index[train_df[\"domain\"] == domain])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "executionInfo": {
     "elapsed": 3,
     "status": "ok",
     "timestamp": 1623212238542,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "vzBQPv0c_Mom"
   },
   "outputs": [],
   "source": [
    "indexes_2 = []\n",
    "for domain in domains_2:\n",
    "    indexes_2 += list(train_df.index[train_df[\"domain\"] == domain])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "executionInfo": {
     "elapsed": 2,
     "status": "ok",
     "timestamp": 1623212239011,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "UAnu4I8__Nae"
   },
   "outputs": [],
   "source": [
    "indexes_3 = []\n",
    "for domain in domains_3:\n",
    "    indexes_3 += list(train_df.index[train_df[\"domain\"] == domain])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "executionInfo": {
     "elapsed": 288,
     "status": "ok",
     "timestamp": 1623212437632,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "AwFSphPa_TqW"
   },
   "outputs": [],
   "source": [
    "all_indexes = set(indexes_0 + indexes_1 + indexes_2 + indexes_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "executionInfo": {
     "elapsed": 302,
     "status": "ok",
     "timestamp": 1623212489662,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "i0VsBjF-_dWO"
   },
   "outputs": [],
   "source": [
    "train_indexes = [\n",
    "    list(all_indexes - set(indexes_0)),\n",
    "    list(all_indexes - set(indexes_1)),\n",
    "    list(all_indexes - set(indexes_2)),\n",
    "    list(all_indexes - set(indexes_3)),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "executionInfo": {
     "elapsed": 296,
     "status": "ok",
     "timestamp": 1623212589705,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "nQgD8vtBAjqe"
   },
   "outputs": [],
   "source": [
    "val_indexes = [indexes_0, indexes_1, indexes_2, indexes_3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 294,
     "status": "ok",
     "timestamp": 1623212602812,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "3Ktt2BAN_1UT",
    "outputId": "ac5f9294-8078-4fff-9078-f6e5893b009b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3655"
      ]
     },
     "execution_count": 74,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(train_indexes[0] + val_indexes[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "85nGaK4VSLb3"
   },
   "source": [
    "# YOLOv5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "executionInfo": {
     "elapsed": 408,
     "status": "ok",
     "timestamp": 1623212802864,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "8psFxWySKgY3"
   },
   "outputs": [],
   "source": [
    "def encode_boxes(entry):\n",
    "    result = []\n",
    "    boxes = entry[\"BoxesString\"]\n",
    "    if boxes == \"no_box\":\n",
    "        return [\"\"]\n",
    "    boxes_list = [box.split(\" \") for box in boxes.split(\";\")]\n",
    "    for box in boxes_list:\n",
    "        x1, y1, x2, y2 = map(int, box)\n",
    "        x_center = (x1 + x2) / 2048\n",
    "        y_center = (y1 + y2) / 2048\n",
    "        width = (x2 - x1) / 1024\n",
    "        height = (y2 - y1) / 1024\n",
    "        temp = \" \".join([\"0\", str(x_center), str(y_center), str(width), str(height)])\n",
    "        if temp not in result:\n",
    "            result.append(temp)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "executionInfo": {
     "elapsed": 706,
     "status": "ok",
     "timestamp": 1623212823572,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "Ot0sGTjV32_N"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "\n",
    "def create_dir(dir_path):\n",
    "    if os.path.exists(dir_path):\n",
    "        shutil.rmtree(dir_path)\n",
    "    os.mkdir(dir_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "executionInfo": {
     "elapsed": 7271,
     "status": "ok",
     "timestamp": 1623212847150,
     "user": {
      "displayName": "Martinez Stark",
      "photoUrl": "",
      "userId": "15125435938317481871"
     },
     "user_tz": -480
    },
    "id": "66TScHt208Dm"
   },
   "outputs": [],
   "source": [
    "for index in range(4):\n",
    "    create_dir(str(index))\n",
    "    os.mkdir(str(index) + \"/images\")\n",
    "    os.mkdir(str(index) + \"/labels\")\n",
    "    os.mkdir(str(index) + \"/images/train\")\n",
    "    os.mkdir(str(index) + \"/images/val\")\n",
    "    os.mkdir(str(index) + \"/labels/train\")\n",
    "    os.mkdir(str(index) + \"/labels/val\")\n",
    "\n",
    "    current = train_df.iloc[train_indexes[index]]\n",
    "    with open(str(index) + \"/train.txt\", \"w\") as train:\n",
    "        for _, entry in current.iterrows():\n",
    "            train.write(entry[\"image_name\"] + \".png\")\n",
    "            train.write(\"\\n\")\n",
    "            new_name = entry[\"image_name\"] + \".txt\"\n",
    "            new_path = os.path.join(str(index) + \"/labels/train\", new_name)\n",
    "            with open(new_path, \"w\") as f:\n",
    "                for box in encode_boxes(entry):\n",
    "                    f.write(box)\n",
    "                    f.write(\"\\n\")\n",
    "\n",
    "    current = train_df.iloc[val_indexes[index]]\n",
    "    with open(str(index) + \"/val.txt\", \"w\") as val:\n",
    "        for _, entry in current.iterrows():\n",
    "            val.write(entry[\"image_name\"] + \".png\")\n",
    "            val.write(\"\\n\")\n",
    "            new_name = entry[\"image_name\"] + \".txt\"\n",
    "            new_path = os.path.join(str(index) + \"/labels/val\", new_name)\n",
    "            with open(new_path, \"w\") as f:\n",
    "                for box in encode_boxes(entry):\n",
    "                    f.write(box)\n",
    "                    f.write(\"\\n\")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyMlvcBcyDvWSHqnOHbo+3qb",
   "collapsed_sections": [],
   "name": "KFold.ipynb",
   "provenance": [],
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
